//
//  HeadlineController.m
//  cdb1
//
//  Created by KKING on 2016/11/18.
//  Copyright © 2016年 msbx. All rights reserved.
//

#import "HeadlineController.h"
#import <WebKit/WebKit.h>
#import <JavaScriptCore/JavaScriptCore.h>

@interface HeadlineController ()<UIWebViewDelegate>{

    
    NSString *_nowurlString;
}

@property (strong,nonatomic) UIWebView *webView  ;

//判断是否是HTTPS的
@property (nonatomic, assign) BOOL isAuthed;

//返回按钮
@property (nonatomic, strong) UIBarButtonItem *backItem;
//关闭按钮
@property (nonatomic, strong) UIBarButtonItem *closeItem;


@end

@implementation HeadlineController
- (void)viewDidLoad {
    [super viewDidLoad];
    //    self.navigationController.navigationBar.hidden = YES;
    //    CGRect bounds = [UIScreen mainScreen].bounds;
    UIWebView* webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0 , SCREEN_WIDTH, SCREEN_HEIGHT)];
    webView.scalesPageToFit = YES;//自动对页面进行缩放以适应屏幕
    //    webView.detectsPhoneNumbers = YES;
    NSURL* url = [NSURL URLWithString:self.url];//创建URL
    NSURLRequest* request = [NSURLRequest requestWithURL:url];//创建NSURLRequest
    
    webView.delegate = self;
    [webView loadRequest:request];
    self.webView = webView;
    //    [webView goBack];
    //    [webView goForward];
    //    [webView reload];//重载
    //    [webView stopLoading];//取消载入内容
    [self.view addSubview:webView];

    [self addLeftButton];
}


#pragma mark UM统计

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];

    HUDStop;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (BOOL)shouldAutorotate{
    
    return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}


- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    if (request.URL == nil) {
        return NO;
    }
    
    NSMutableString *urlmustring = [[NSMutableString alloc]initWithString:[request URL].absoluteString];
    
    _nowurlString = urlmustring;
    
//    [NSString stringWithFormat:@"%@&ishead=1",[request URL].absoluteString];
//    NSLog(@"%@",[request URL].absoluteString);
    
    return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView{

//    NSString *str = @"$(\".cdb_zddtb\").css(\"display\",\"none\");";
//    [webView stringByEvaluatingJavaScriptFromString:str];
    HUDLoading(@"加载中...");
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        HUDStop;
    });
}



- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//    NSString *str = @"\"document.getElementsByClassName('cdb_zddtb').style.display=\"none\";";
//    NSString *str = @"$(\".cdb_zddtb\").css(\"display\",\"none\");";
//    [webView stringByEvaluatingJavaScriptFromString:str];
//    NSString *jsString = [NSString stringWithFormat:@"window.location.href = '#%@'",self.nwUrlString];
//    [webView stringByEvaluatingJavaScriptFromString:jsString];
    
    //除了启动闪屏广告位之外的url
    
    if (self.title != nil) {
        return;
    }
    
    //设置当前页面的title
        self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];

    HUDStop;
//    if ([_nowurlString containsString:@"alwcoursedetail"]) {
//            [self.webView goBack];
//    }
}


#pragma mark - 添加关闭按钮
- (void)addLeftButton
{
    self.navigationItem.leftBarButtonItems = @[self.backItem];
}

//点击返回的方法
- (void)backNative
{
    //判断是否有上一层H5页面
    if ([self.webView canGoBack]) {
        //如果有则返回
        [self.webView goBack];
        //同时设置返回按钮和关闭按钮为导航栏左边的按钮
        self.navigationItem.leftBarButtonItems = @[self.backItem, self.closeItem];
    } else {
        [self closeNative];
    }
}

//关闭H5页面，直接回到原生页面
- (void)closeNative
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - init

- (UIBarButtonItem *)backItem
{
    if (!_backItem) {
        _backItem = [[UIBarButtonItem alloc] init];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        //这是一张“<”的图片，可以让美工给切一张
        UIImage *image = [UIImage imageNamed:@"icon_back"];
        [btn setImage:image forState:UIControlStateNormal];
        [btn setImage:image forState:UIControlStateHighlighted];

//        [btn setTitle:@"返回" forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(backNative) forControlEvents:UIControlEventTouchUpInside];
//        [btn.titleLabel setFont:[UIFont systemFontOfSize:17]];
//        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        //字体的多少为btn的大小
        [btn sizeToFit];
        //左对齐
        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        //让返回按钮内容继续向左边偏移15，如果不设置的话，就会发现返回按钮离屏幕的左边的距离有点儿大，不美观
//        btn.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
        btn.frame = CGRectMake(0, 0, 30, 42);
        _backItem.customView = btn;
    }
    return _backItem;
}

- (UIBarButtonItem *)closeItem
{
    if (!_closeItem) {
        _closeItem = [[UIBarButtonItem alloc] init];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [btn setTitle:@"关闭" forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(closeNative) forControlEvents:UIControlEventTouchUpInside];
//        [btn.titleLabel setFont:[UIFont systemFontOfSize:17]];
//        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        //字体的多少为btn的大小
        [btn sizeToFit];
        //左对齐
        UIImage *image = [UIImage imageNamed:@"guanbi"];
        [btn setImage:image forState:UIControlStateNormal];
        [btn setImage:image forState:UIControlStateHighlighted];

//        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        btn.frame = CGRectMake(0, 0, 45, 38);
        _closeItem.customView = btn;
    }
    return _closeItem;
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
    //UIStatusBarStyleDefault = 0 黑色文字，浅色背景时使用
    //UIStatusBarStyleLightContent = 1 白色文字，深色背景时使用
}



@end
